#!/bin/bash
#Author: YangYi
#Date: 2023-02-21
#Version: v1

#格式化时间
TIME=`date +%F-%H:%M:%S`

#接受者邮箱
#EMAIL_RECIVER="973534823@qq.com"
EMAIL_RECIVER="oyhh71087475.sh@chinatelecom.cn jinjiaxin@tontron.com.cn yanglanglang@ideal.sh.cn huangjm17@chinatelecom.cn xiexp2@chinatelecom.cn tongfy@chinatelecom.cn yangyi@tontron.com.cn zhangjie3@ideal.sh.cn yutao1@ideal.sh.cn wangpeng1@ideal.sh.cn tangcy@ideal.sh.cn  guodc@ideal.sh.cn zhusj7@chinatelecom.cn guodechao.sh@chinatelecom.cn zhangj119@chinatelecom.cn"

#接受者短信
#PAAS_PHONE=["13564881037"]
PAAS_PHONE=["18939860223","18338822990","13227009978","17783795215","17758677587","19916970183","13564881037","17635570615","13513842121","18800295579","18317519326","17321261159","13381729139","19916970139"]

#集群nameserver列表
ALLNAMESERVERLIST=`sudo /usr/local/bin/kubectl  get  pods --all-namespaces -o wide | grep nameserver |awk '{print $1"-"$2":"$7}'`

#文件过滤路径
FILTER_FILE=/app/paasadmin/NameserverCheck/
/bin/mkdir -p  $FILTER_FILE && chown -R paasadmin:paasadmin $FILTER_FILE

#存储路径
FILE_PATH=$FILTER_FILE/$TIME-NameserverCheck.txt

#临时文件
TEMPORARY_FILE=/app/paasadmin/NameserverList.txt

#设置要保存的文件个数
SAVE_COUNT="48"

NAMELIST=$()

#Nameserver列表
function ServerNameList(){
  if [  -f $TEMPORARY_FILE ];then
    rm -f $TEMPORARY_FILE
  fi
  for serverlist in $ALLNAMESERVERLIST
    do
      echo $serverlist >> $TEMPORARY_FILE
    done
}

#Nameserver存活探测
function ServerNameCheck(){
  #发送者邮箱
  email_sender="g-paas_monitor.sh@chinatelecom.cn"
  #邮箱用户名
  email_username="g-paas_monitor.sh@chinatelecom.cn"
  #邮箱密码
  #使用qq邮箱进行发送需要注意：首先需要开启：POP3/SMTP服务，其次发送邮件的密码需要使用在开启POP3/SMTP服务时候腾讯提供的第三方客户端登陆码。
  email_password="3xT)zQKUf!L5vzKN"
  #email_password=Jiekou@12
  #smtp服务器地址
  email_smtphost=smtp.chinatelecom.cn
  email_title="天翼云生产注册中心存活探测告警"

  for ip in `cat $TEMPORARY_FILE | awk -F ":" '{print $2}'`
    do
      curl -s  --connect-timeout 1  $ip:8761/health >> /dev/null 
      if [ $? != 0 ];then
         cat $TEMPORARY_FILE | grep $ip | awk -F "-" '{print "环境:"$1"|引擎:"$2"|服务:"$4}' >> $FILE_PATH 
         NAMELIST=$(cat $TEMPORARY_FILE | grep $ip | awk -F "-" '{print "\"注册中心服务告警:环境:"$1"|引擎:"$2"|服务:"$4"\""}')
         curl http://10.145.167.219:8070/plat/shtelpaassms/send  -X POST -H "Content-type:application/json" -d '{"phone":'$PAAS_PHONE',"msg":'$NAMELIST',"userName":"test","pwd":"123456"}'
         sleep 0.5
         sudo /usr/local/bin/sendEmail  -f ${email_sender} -t ${EMAIL_RECIVER} -s ${email_smtphost} -u ${email_title} -xu ${email_username} -xp ${email_password} -m ${NAMELIST} -a ${file1_path} ${file2_path} -o tls=no -o message-charset=utf-8
         #curl http://10.145.167.219:8070/plat/shtelpaassms/send  -X POST -H "Content-type:application/json" -d '{"phone":["13564881037"],"msg":'$NAMELIST',"userName":"test","pwd":"123456"}'
      fi
    done
}

#文件清理
function File_Cleanup(){
cd $FILTER_FILE
#1 判断文件个数
count=$(ls -l | wc -l)
delcount=$[$count - $SAVE_COUNT]
#2 是否进行删除
if [ $delcount -gt 0 ] ; then
        #获取时间最久的文件夹名称
        filesname=$(ls -tr | head -n $delcount)
        filesname=${filesname// /}
        filesarr=($filesname)
        #循环依次删除时间久的文件
        for each in ${filesarr[*]}
          do
            rm -rf $each
          done
fi
}

ServerNameList
ServerNameCheck
File_Cleanup









